package switchviz.graphic.gauge.graph2d;

import org.jdom.Element;

import switchviz.graphic.gauge.GaugePersistence;
import switchviz.graphic.gauge.graph2d.Graph2dDataModel.AlarmMode;
import switchviz.graphic.gauge.graph2d.Graph2dDataModel.ScaleMode;

public class Graph2dPersistence extends GaugePersistence
{
	
	public static final String DATAMIN = "datamin";
	public static final String DATAMAX = "datamax";
	public static final String POINTSIZE = "pointsize";
	public static final String DATALENGTH = "datalength";
	public static final String GRANULARITY = "granularity";
	public static final String ALARMMODE = "alarmmode";
	private final static String SCALEMODE = "scalemode";
	
	public Graph2dPersistence(Element root)
	{
		_root = root;
	}
	
	public Graph2dDataModel getDataModel(Element root)
	{
		_root = root;
		
		return getDataModel();
	}
	
	public Graph2dDataModel getDataModel()
	{
		Graph2dDataModel datamodel = new Graph2dDataModel();
		super.setDataModel(datamodel);
		if(_root.getChild(DATAMIN) != null)
			datamodel.setDataMin(Float.valueOf(_root.getChildText(DATAMIN)));
		if(_root.getChild(DATAMAX) != null)
			datamodel.setDataMax(Float.valueOf(_root.getChildText(DATAMAX)));
		if(_root.getChild(POINTSIZE) != null)
			datamodel.setPointsize(Float.valueOf(_root.getChildText(POINTSIZE)));
		if(_root.getChild(DATALENGTH) != null)
			datamodel.setDataLength(Integer.valueOf(_root.getChildText(DATALENGTH)));
		if(_root.getChild(GRANULARITY) != null)
			datamodel.setGranularity(Integer.valueOf(_root.getChildText(GRANULARITY)));
			
		if(_root.getChild(ALARMMODE) != null)
		{
			if (_root.getChildText(ALARMMODE).equals("None"))
				datamodel.setAlarmMode(AlarmMode.NONE);
		}
		
		if(_root.getChild(SCALEMODE) != null)
		{
			if (_root.getChildText(SCALEMODE).equals("linear"))
				datamodel.setScaleMode(ScaleMode.LINEAR);
			else if (_root.getChildText(SCALEMODE).equals("logarithm"))
				datamodel.setScaleMode(ScaleMode.LOGARITHM);
		}
		
		return datamodel;
	}
	
	public Element getElement(Graph2dDataModel datamodel)
	{
		super.getElement(datamodel);
		addNode(DATAMIN, String.valueOf(datamodel.getDataMin()));
		addNode(DATAMAX, String.valueOf(datamodel.getDataMax()));
		addNode(DATALENGTH, String.valueOf(datamodel.getDataLength()));
		addNode(GRANULARITY, String.valueOf(datamodel.getGranularity()));
		addNode(POINTSIZE, String.valueOf(datamodel.getPointsize()));
		
		switch (datamodel.getAlarmMode())
		{
		case NONE:
			addNode(ALARMMODE, "None");
			break;
		}
		
		switch(datamodel.getScaleMode())
		{
		case LINEAR:
			addNode(SCALEMODE, "linear");
			break;
		case LOGARITHM:
			addNode(SCALEMODE, "logarithm");
			break;
		}
		
		return _root;
	}
}
